/**   
* @Title: AppSystemServiceImpl.java 
* @Package com.nebula.upms.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author zhanghui   
* @date 2016年2月3日 下午1:49:23 
* @version V1.0   
*/
package com.nebula.upms.service.biz.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nebula.common.util.UUIDGenerator;
import com.nebula.upms.service.biz.AppSystemService;
import com.nebula.upms.service.model.po.*;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nebula.common.framework.BaseService;


/** 
* @ClassName: AppSystemServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author zhanghui
* @date 2016年2月3日 下午1:49:23 
*  
*/
@Service("appSystemService")
@Transactional
public class AppSystemServiceImpl extends BaseService implements AppSystemService {

	/*
	 * <p>Title: saveAppSystem</p>
	 * <p>Description: </p>
	 * @param params
	 * @see com.nebula.upms.service.AppSystemService#saveAppSystem(java.util.Map)
	 */
	@Override
	public void saveAppSystem(UpmsAppSystemPO UpmsAppSystemPO, String[] appNameArray, String[] appServerNameArray, String[] appPortArray) {
		UpmsAppSystemPOExample example=new UpmsAppSystemPOExample();
		example.setOrderByClause(" priority DESC");
		UpmsAppSystemPO lastUpmsAppSystemPO= UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(example);
		int priority=0;
		if(null!=lastUpmsAppSystemPO&&null!=lastUpmsAppSystemPO.getPriority()){
			priority=lastUpmsAppSystemPO.getPriority()+1;
		}
		
		UpmsAppSystemPO.setAppSystemId(UUIDGenerator.random32UUID());
		UpmsAppSystemPO.setCreateTime(new Date().getTime());
		UpmsAppSystemPO.setIsDelete(false);
		UpmsAppSystemPO.setPriority(priority);
		//UpmsAppSystemPO.setIcon("fa "+UpmsAppSystemPO.getIcon());
		UpmsDs.instance().mybatis().ds().mapper().insert(UpmsAppSystemPO);
		
		for (int i=0;i<appNameArray.length;i++) {
			UpmsAppPO UpmsAppPO=new UpmsAppPO();
			UpmsAppPO.setAppId(UUIDGenerator.random32UUID());
			UpmsAppPO.setAppSystemId(UpmsAppSystemPO.getAppSystemId());
			UpmsAppPO.setName(appNameArray[i]);
			UpmsAppPO.setAppServerName(appServerNameArray[i]);
			UpmsAppPO.setPort(Integer.valueOf(appPortArray[i]));
			UpmsAppPO.setPriority(i);
			UpmsAppPO.setIsDelete(false);
			UpmsAppPO.setCreateTime(new Date().getTime());
			UpmsDs.instance().mybatis().ds().mapper().insert(UpmsAppPO);
		}
		
	}

	/*
	 * <p>Title: getUpmsAppSystemPOByKey</p>
	 * <p>Description: </p>
	 * @param appSystemId
	 * @return
	 * @see com.nebula.upms.service.AppSystemService#getUpmsAppSystemPOByKey(java.lang.String)
	 */
	@Override
	public UpmsAppSystemPO getUpmsAppSystemPOByKey(String appSystemId) {
		
		return UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppSystemPO.class, appSystemId);
	}

	/*
	 * <p>Title: updateAppSystem</p>
	 * <p>Description: </p>
	 * @param UpmsAppSystemPO
	 * @param appNameArray
	 * @param appServerNameArray
	 * @param appPortArray
	 * @see com.nebula.upms.service.AppSystemService#updateAppSystem(com.hbdiy.common.model.po.UpmsAppSystemPO, java.lang.String[], java.lang.String[], java.lang.String[])
	 */
	@Override
	public void updateAppSystem(UpmsAppSystemPO UpmsAppSystemPO,String[] appIdArray, String[] appNameArray, String[] appServerNameArray,
			String[] appPortArray) {
		UpmsAppSystemPO.setUpdateTime(new Date().getTime());
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsAppSystemPO);
		for (int i=0;i<appIdArray.length;i++) {
			String appId=appIdArray[i];
			if(!appId.equals("default")){//xiugai
				UpmsAppPO UpmsAppPO=new UpmsAppPO();
				UpmsAppPO.setAppId(appId);
				UpmsAppPO.setAppSystemId(UpmsAppSystemPO.getAppSystemId());
				UpmsAppPO.setName(appNameArray[i]);
				UpmsAppPO.setAppServerName(appServerNameArray[i]);
				UpmsAppPO.setPort(Integer.valueOf(appPortArray[i]));
				UpmsAppPO.setPriority(i);
				UpmsAppPO.setIsDelete(false);
				UpmsAppPO.setUpdateTime(new Date().getTime());
				UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsAppPO);
			}else{//xinzeng
				UpmsAppPO UpmsAppPO=new UpmsAppPO();
				UpmsAppPO.setAppId(UUIDGenerator.random32UUID());
				UpmsAppPO.setAppSystemId(UpmsAppSystemPO.getAppSystemId());
				UpmsAppPO.setName(appNameArray[i]);
				UpmsAppPO.setAppServerName(appServerNameArray[i]);
				UpmsAppPO.setPort(Integer.valueOf(appPortArray[i]));
				UpmsAppPO.setPriority(i);
				UpmsAppPO.setIsDelete(false);
				UpmsAppPO.setCreateTime(new Date().getTime());
				UpmsDs.instance().mybatis().ds().mapper().insert(UpmsAppPO);
			}
		}
	}

	/*
	 * <p>Title: moveup</p>
	 * <p>Description: </p>
	 * @param appSystemId
	 * @see com.nebula.upms.service.AppSystemService#moveup(java.lang.String)
	 */
	@Override
	public void moveup(String appSystemId) {
		UpmsAppSystemPO UpmsAppSystemPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppSystemPO.class,appSystemId);
		UpmsAppSystemPOExample example =new UpmsAppSystemPOExample();
		example.setOrderByClause("priority ASC");
		List<UpmsAppSystemPO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
		int index=0;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getAppSystemId().equals(appSystemId)){
				if(i==0){
					index=0;
				}else{
					index=i-1;
				}
				break;
			}
		}
		UpmsAppSystemPO prev=list.get(index);
		int priority=prev.getPriority();
		prev.setPriority(UpmsAppSystemPO.getPriority());
		UpmsAppSystemPO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(prev);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsAppSystemPO);
	}

	/*
	 * <p>Title: movedown</p>
	 * <p>Description: </p>
	 * @param appSystemId
	 * @see com.nebula.upms.service.AppSystemService#movedown(java.lang.String)
	 */
	@Override
	public void movedown(String appSystemId) {
		UpmsAppSystemPO UpmsAppSystemPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppSystemPO.class,appSystemId);
		UpmsAppSystemPOExample example =new UpmsAppSystemPOExample();
		example.setOrderByClause("priority ASC");
		List<UpmsAppSystemPO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
		int index=0;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getAppSystemId().equals(appSystemId)){
				if(i==(list.size()-1)){
					index=list.size()-1;
				}else{
					index=i+1;
				}
				break;
			}
		}
		UpmsAppSystemPO next=list.get(index);
		int priority=next.getPriority();
		next.setPriority(UpmsAppSystemPO.getPriority());
		UpmsAppSystemPO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(next);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsAppSystemPO);
		
	}

	/*
	 * <p>Title: removeAppSystem</p>
	 * <p>Description: </p>
	 * @param appSystemId
	 * @return
	 * @see com.nebula.upms.service.AppSystemService#removeAppSystem(java.lang.String)
	 */
	@Override
	public boolean removeAppSystem(String appSystemId) {
		Map<String,Object> params=new HashMap<>();
		params.put("appSystemId", appSystemId);
		int count=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectOne("selectCountAppSystemUsedPrivilege",params);
		if(count>0){
			return false;
		}
		UpmsPrivilegePOExample UpmsPrivilegePOExample=new UpmsPrivilegePOExample();
		UpmsPrivilegePOExample.createCriteria().andPrivilegePidIsNull().andAppSystemIdEqualTo(appSystemId);
		UpmsPrivilegePO root=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(UpmsPrivilegePOExample);
		
		//递归删除权限
		this.deletePrivilege(root.getPrivilegeId());
		//删除应用
		UpmsAppPOExample UpmsAppPOExample=new UpmsAppPOExample();
		UpmsAppPOExample.createCriteria().andAppSystemIdEqualTo(appSystemId);
		UpmsDs.instance().mybatis().ds().mapper().deleteByExample(UpmsAppPOExample);
		//删除应用系统
		UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsAppSystemPO.class, appSystemId);
		
		return true;
	}
	
	private void deletePrivilege(String privilegeId ){
		UpmsPrivilegePOExample example=new UpmsPrivilegePOExample();
		example.createCriteria().andPrivilegePidEqualTo(privilegeId);
		List<UpmsPrivilegePO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
		if(list.size()>0){
			for (UpmsPrivilegePO UpmsPrivilegePO : list) {
				deletePrivilege(UpmsPrivilegePO.getPrivilegeId());
			}
		}
		UpmsPrivilegeOperatePOExample UpmsPrivilegeOperatePOExample=new UpmsPrivilegeOperatePOExample();
		UpmsPrivilegeOperatePOExample.createCriteria().andPrivilegeIdEqualTo(privilegeId);
		UpmsDs.instance().mybatis().ds().mapper().deleteByExample(UpmsPrivilegeOperatePOExample);
		
		UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsPrivilegePO.class,privilegeId);
	}
	
}
